var commonObj = {

    init: function () {

        //引入外部文件
        this.setExternalFiles();
    },

    //CSS文件数组
    cssFileArr: [
        "/jijianzw/css/jquery.pagination/jquery.pagination.css"
    ],

    //JS文件数组
    jsFileArr: [
        "/jijianzw/scripts/jquery.cookie/jquery.cookie.js",
        "/jijianzw/scripts/jquery.pagination/jquery.pagination.js",
        "/jijianzw/scripts/jquery.json/jquery.json.js",
        //"/jijianzw/scripts/kit_loadingBar/kit_loadingBar.js",
        "/jijianzw/scripts/loadingBar/loadingBar.js"
    ],

    //引入CSS文件
    setCssFile: function (file_path) {
        var e = document.createElement("link");
        e.rel = "stylesheet";
        e.href = file_path;
        document.getElementsByTagName("head")[0].appendChild(e);
    },

    //引入JS文件
    setJsFile: function (file_path) {
        document.write("<script src='" + file_path + "'></script>");
    },

    //引入外部文件
    setExternalFiles: function () {
        var i;

        //引入JS文件
        for (i = 0; i < this.jsFileArr.length; i++) {
            this.setJsFile(this.jsFileArr[i]);
        }

        //引入CSS文件
        for (i = 0; i < this.cssFileArr.length; i++) {
            this.setCssFile(this.cssFileArr[i]);
        }
    },

    //xdomainrequest 为了解决IE9及其以下版本的AJAX跨域问题
    transportType: "",

    //绝对居中操作
    set_middle: function (target, parent, type) {
        var parent_width = $(parent).width();
        var target_width = $(target).outerWidth();
        var parent_height = $(parent).height();
        var target_height = $(target).outerHeight();


        //只需要水平居中
        if (type == 1) {
            $(target).css({
                "left": (parent_width - target_width) / 2
            });
        }

        //只需要垂直居中
        if (type == 2) {
            $(target).css({
                "top": (parent_height - target_height) / 2
            });
        }

        //水平垂直都需要居中
        if (type == 3) {
            $(target).css({
                "left": (parent_width - target_width) / 2,
                "top": (parent_height - target_height) / 2
            });
        }
    },

    //获取url参数
    get_param: function () {
        var url = decodeURI(location.search); //获取url中"?"符后的字串
        var param = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                param[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
            }
        }
        return param;
        // 用法：param_value = get_param()["param_mame"];
    },

    //滚动条回到顶部 str="id/class";
    back_to_top: function (str) {
        $(str).animate({scrollTop: 0}, 500);
    },


    //滚动条回到底部 str="id/class";
    back_to_bottom: function (str) {
        $(str).animate({scrollTop: 99999999}, 500);
    },

    //输入框只能输入数字
    only_num: function (class_name) {
        $(class_name).on("keyup", function () {
            $(this).val($(this).val().replace(/[^0-9]/g, ''));
        }).css("ime-mode", "disabled");
    },

    //对时间进行格式化
    get_format_date: function (date) {
        var format_date = this.add_zero_time(date.getFullYear()) + "-" + this.add_zero_time(date.getMonth() + 1) + "-" + this.add_zero_time(date.getDate()) + " " + this.add_zero_time(date.getHours()) + ":" + this.add_zero_time(date.getMinutes()) + ":" + this.add_zero_time(date.getSeconds());
        return format_date;
    },

    //小于10的时间前补0
    add_zero_time: function (time) {
        if (time >= 0 && time <= 9) {
            time = "0" + time;
        }
        return time;
    },

    //计算两个日期的间隔天数
    calculate_date_interval: function (date_a, date_b) {
        var date_1 = new Date(date_a);
        var date_2 = new Date(date_b);
        var result_days = Math.round(Math.abs(date_1 - date_2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
        return result_days;
    },

    //处理值为null的字段
    deal_text_null: function (str) {
        return (str == null || str == "null" || str == "") ? "无" : str;
    },

    //把秒转化为时-分-秒格式
    formatSeconds: function (value) {
        var theTime = parseInt(value);// 秒
        var theTime1 = 0;// 分
        var theTime2 = 0;// 小时
        if (theTime > 60) {
            theTime1 = parseInt(theTime / 60);
            theTime = parseInt(theTime % 60);
            if (theTime1 > 60) {
                theTime2 = parseInt(theTime1 / 60);
                theTime1 = parseInt(theTime1 % 60);
            }
        }
        var result = "" + parseInt(theTime) + "秒";
        if (theTime1 > 0) {
            result = "" + parseInt(theTime1) + "分" + result;
        }
        if (theTime2 > 0) {
            result = "" + parseInt(theTime2) + "小时" + result;
        }
        return result;
    },

    //对时间进行格式化
    get_format_date_no_time: function (date) {
        return this.add_zero_time(date.getFullYear()) + "-" + this.add_zero_time(date.getMonth() + 1) + "-" + this.add_zero_time(date.getDate());
    },

    //利用空对象作为中介继承
    extend: function (Child, Parent) {
        var F = function () {
        };
        F.prototype = Parent.prototype;
        Child.prototype = new F();
        Child.prototype.constructor = Child;
        Child.uber = Parent.prototype;
    },

    //深拷贝对象
    deepCopy: function (source) {
        var result = {};
        for (var key in source) {
            result[key] = typeof source[key] === "object" ? this.deepCopy(source[key]) : source[key];
        }
        return result;
    },

    //深拷贝对象表层模块
    deepCopyModule: function (source) {
        var result = {};
        for (var key in source) {
            result[key] = source[key];

        }
        return result;
    },

    //检测是否移动端设备
    detectmob: function () {
        if (navigator.userAgent.match(/Android/i)
            || navigator.userAgent.match(/webOS/i)
            || navigator.userAgent.match(/iPhone/i)
            || navigator.userAgent.match(/iPad/i)
            || navigator.userAgent.match(/iPod/i)
            || navigator.userAgent.match(/BlackBerry/i)
            || navigator.userAgent.match(/Windows Phone/i)
        ) {
            return true;
        }
        else {
            return false;
        }
    },

    //控制session过期操作只出现一次
    sessionTimeOutAlertFlag: 0,

    //验证是否session过期
    isSessionTimeOut: function (sessionTimeOut, callback) {
        if (!sessionTimeOut) {
            this.sessionTimeOutAlertFlag = 0;

            callback();
        } else {
            if (!this.sessionTimeOutAlertFlag) {

                this.sessionTimeOutAlertFlag = 1;

                alert("因为您长时间未操作，登录信息已过期，请重新登录后再进行操作");

                window.location.href = "/jijianzw/webpage/login/timeout.jsp";
            }
        }
    },

    //去除字符串左右两端的空格
    trim: function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },

    //去除字符串左端的空格
    ltrim: function (str) {
        return str.replace(/(^\s*)/g, "");
    },

    //去除字符串右端的空格
    rtrim: function (str) {
        return str.replace(/(\s*$)/g, "");
    },

    //显示进度条
    loadingBarShow: function (text, time) {
        kit_loadingBar.show(text, time);

        //A.showMask(text);
    },

    //隐藏进度条
    loadingBarHide: function () {
        kit_loadingBar.hide();

        //A.hideMask();
    },

    //通过session获得当前用户信息
    getUserInfoBySession: function (callback) {

        $.ajax({
            type: "post",
            dataType: "json",
            url: "/jijianzw/api/weixinController.do?getSession",
            cache: false,//不缓存
            async: false,
            data: {},
            success: function (data) {

                //console.log(data);

                commonObj.isSessionTimeOut(data.sessionTimeOut, function () {

                    data = data.data;

                    //console.log(data);

                    if (callback) {
                        callback(data);
                    }
                });
            }
        });
    },

    //禁止页面后退
    disableHistoryBack: function () {

        history.pushState(null, null, document.URL);

        window.addEventListener("popstate", function () {
            history.pushState(null, null, document.URL);
        });
    }
};

commonObj.init();